package com.gaohe.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.gaohe.common.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

//mybatisPlus字段自动填充功能
//只会对Mp的API生效
@Component//被Spring容器管理
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
//新增时生效
    @Override
    public void insertFill(MetaObject metaObject) {
     log.info("字段填充：{}",metaObject);
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createTime",LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
        metaObject.setValue("createUser", BaseContext.getCurrentId());
    }
//修改时生效
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("字段填充：{}",metaObject);
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());


    }
}
